const fs = require('fs')
const path = require('path')
const babelParser = require('@babel/parser')
const traverse = require('@babel/traverse').default
const {transformFromAst} = require('@babel/core')


const parser = {
    //将文件解析成ast
    getAst(filePath){
        //1.读取入口文件内容
        //入口文件路径
        const file = fs.readFileSync(filePath,'utf-8')
        //2.将其解析成ast抽象语法树
        const ast = babelParser.parse(file,{
            sourceType:'module' //解析文件时es模块
        })
        return ast;
    },

    //获取依赖
    getDeps(ast,filePath){
        //获取入口文件的文件夹路径
        const dirname = path.dirname(filePath)
        //定义存储依赖的容器
        const dep = {}

        //3.收集依赖
        traverse(ast,{
            //内部会遍历ast中program.body,判断里面语句类型
            //如果type.ImportDeclaration 就会触发当前函数
            ImportDeclaration({node}){
                //文件相对路径 ./add.js
                const relativePath = node.source.value
                //生成基于入口文件的绝对路径
                const absolutePath = path.resolve(dirname,relativePath)
                //添加依赖
                dep[relativePath] = absolutePath;
            }
        })

        return dep;
    },
    //将ast解析成code
    getCode(ast){
        //4.编译代码：将代码中浏览器不能识别的语法进行编译
        const {code} = transformFromAst(ast,null,{
            presets:['@babel/preset-env']
        })
        return code;
    }
}

module.exports = parser